package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pojo.MesFeedback;
import com.ruoyi.service.MesFeedbackService;
import com.ruoyi.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/feedback")
@CrossOrigin
public class FeedbackController {
    @Autowired
    private MesFeedbackService service;

    @GetMapping("/test")
    public void test(){
        System.out.println("right");
    }


    @GetMapping("/list")
    public R<List<MesFeedback>> list(MesFeedback mes){
        List<MesFeedback> list;
        if (mes != null) {
            QueryWrapper wq = new QueryWrapper();
            System.out.println(mes);
            System.out.println(mes.getTitle());
            if (mes.getUsername()!=null&&!mes.getUsername().equals("")) {
                wq.like("username",mes.getUsername());
            }
            if (mes.getTitle()!=null&&!mes.getTitle().equals("")) {
                wq.like("title",mes.getTitle());
            }
            if (mes.getId()!=null&&!mes.getId().equals("")) {
                wq.eq("id",mes.getId());
            }
            list = service.list(wq);
        }else{
            list = service.list();
        }
        System.out.println(list);
        return R.success(list);
    }
    @PostMapping("/update")
    public R<String> update(MesFeedback mes){
        boolean b = service.updateById(mes);
        if (b){
            return R.success("修改成功！");
        }else {
            return R.error("修改失败！");
        }
    }
    @PostMapping("/add")
    public R<String> add(@RequestBody MesFeedback mes){
        Date date = new Date();
        mes.setFeedbackTime(date);
        String username = SecurityUtils.getUsername();
        mes.setUsername(username);
        int i = service.add(mes);
        if (i>0){
            return R.success("新增成功！");
        }else {
            return R.error("新增失败！");
        }
    }
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") int id){
        boolean b = service.removeById(id);
        if (b){
            return R.success("删除成功！");
        }else {
            return R.error("删除失败！");
        }
    }


}
